#include<stdio.h>
#include"linklist.h"

//初始化一个不带头节点的链表   head -> NULL
linklist init_list(void)
{
    return NULL;
}

//新建节点
linklist new_node(int data)
{
    linklist new = malloc(sizeof(linknode));
    if(new != NULL){
        new->data = data;
        new->next = NULL;
    }
    return new;
}

void list_add_tail(linklist *phead, linklist new)
{
    if(*phead == NULL){
        *phead = new;
        return;
    }

    linklist tail;
    for(tail = *phead; tail->next != NULL; tail = tail->next);

    tail->next = new;
}

linklist list_del(linklist *phead, int data)
{
    if(*phead == NULL) //头节点为空
        return NULL;

    if((*phead)->data == data){ //第一个节点为目标(情况1：无后续节点，情况2：有后续节点)
        linklist tmp = *phead;
        *phead = (*phead)->next;
        return tmp;
    }

    linklist k, p;
    for(k = *phead; k->next != NULL && k->next->data != data; k = k->next);

    if(k->next == NULL)//整个链表没有目标
        return NULL;

    p = k->next;
    k->next = p->next;
    p->next = NULL;
    return p;
}

linklist list_for_each(linklist head)
{
    if(head == NULL)
        return;
    for(linklist p = head; p != NULL; p = p->next){
        printf("%d\t", p->data);  
    }
    printf("\n");
}